#include "tankmodel.h"
#include <QDebug>
#include <GL/gl.h>
#include <cmath>

const double TankModel::angleChangeStep_ = 1.0;
const double TankModel::angleChangeStepShifted_ = 5.0;

TankModel::TankModel() : pos_(0.0), angle_(45.0)
{
}

void TankModel::render()
{
    glPushMatrix();
    glTranslated(-pos_, 0.0, 0.0);

    double halfWidth = width_ / 2.0;

    glColor3f(.9, .9, .9);

    glBegin(GL_QUADS);
        //bottom
        glVertex2d(-halfWidth, 0);
        glVertex2d(halfWidth, 0);
        //top
        glVertex2d(halfWidth, height_);
        glVertex2d(-halfWidth, height_);
    glEnd();

    glPushMatrix();
    //start drawing from the center of the tank body
    glTranslated(0.0, height_/2.0, 0.0);
    glColor3f(.7, .3, .0);
    glBegin(GL_LINES);
        glVertex2d(0.0, 0.0);
        glVertex2i(barrelX(), barrelY());
    glEnd();
    glPopMatrix();

    glPopMatrix();
}

void TankModel::explode()
{
}

void TankModel::increaseAngle(bool shifted)
{
    if(angle_ < 180.0) {
        angle_ += shifted ? angleChangeStepShifted_ : angleChangeStep_;
    }

    if(angle_ > 180.0) {
        angle_ = 180.0;
    }

    qDebug() << "Barrel angle: " << angle_;
}

void TankModel::decreaseAngle(bool shifted)
{
    if(angle_ > 0.0) {
        angle_ -= shifted ? angleChangeStepShifted_ : angleChangeStep_;
    }

    if(angle_ < 0.0) {
        angle_ = 0.0;
    }

    qDebug() << "Barrel angle: " << angle_;
}

/**************************************************************************************************/

Projectile* Player::fire(double maxRange)
{
    double yInit = (height_ / 2.0) + barrelY();
    return new Projectile(angle_, maxRange, barrelX()-pos_, yInit);
}
